package com.sgb.kjwl.view.widget;

import android.content.Context;
import androidx.recyclerview.widget.GridLayoutManager;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sgb.kjwl.R;
import com.sgb.link.entity.AreaEntity;
import com.sgb.jicai.entity.FilterEntity;
import com.sgb.kjwl.view.ui.adapter.power.PowerAreaAdapter;
import com.sgb.kjwl.view.ui.adapter.power.PowerTypeAdapter;
import com.swgk.core.customview.MaxRecyclerView;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.MLog;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class BuildPowerPopWindow extends BasePopWindow {
    private LinearLayout rootLayout;
    private MaxRecyclerView recyclerView;
    private MaxRecyclerView childView;
    private TextView okView;
    private TextView resetView;
    private FliterPopWindow.FilterResultClick filterResultClick;
    private List<FilterEntity> filterEntityList;
    private List<AreaEntity> areaEntityList;
    private PowerAreaAdapter powerAreaAdapter;
    private List<PowerTypeAdapter> powerTypeAdapterList;
    private PowerTypeAdapter powerTypeAdapter;
    private List<String> noList;
    private Map map;
    //第一级分类编号
    private String pNo = "";
    //第一级分类编号
    private String dNo = "";
    //第一级分类编号
    private String eNo = "";

    public BuildPowerPopWindow(Context context) {
        super(context, DIR_DEFUALT);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_power;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        rootLayout = holder.getPop().findViewById(R.id.root_layout);
        recyclerView = holder.getPop().findViewById(R.id.filter_list);
        childView = holder.getPop().findViewById(R.id.child_list);
        okView = holder.getPop().findViewById(R.id.ok_view);
        resetView = holder.getPop().findViewById(R.id.reset_view);
        okView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dissMiss();
                if (onResultClick != null) {
                    if (noList == null) {
                        noList = new ArrayList<>();
                    }
                    noList.clear();
                    noList.add(dNo);
                    noList.add(eNo);
                    onResultClick.result(pNo, noList,null);
                }
            }
        });
        resetView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //第一级分类编号
                pNo = "";
                dNo = "";
                eNo = "";
//                rootLayout.removeAllViews();
                powerAreaAdapter.setDefItem(-1);
                for (PowerTypeAdapter powerTypeAdapter : powerTypeAdapterList) {
                    powerTypeAdapter.setDefItem(-1);
                }
            }
        });
        powerAreaAdapter = new PowerAreaAdapter(mContext, null);
        powerAreaAdapter.setItemSelect(true);
        powerAreaAdapter.setDefItem(-1);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        recyclerView.setAdapter(powerAreaAdapter);
        powerAreaAdapter.setOnItemClick(new AdapterOnItemClick<AreaEntity>() {
            @Override
            public void onItemClick(AreaEntity areaEntity, int position) {
                pNo = areaEntity.getNo();
                /*filterAdapter.setDefItem(0);
                filterAdapter.setDatas(listFilterEntity.getChildren());
                MLog.d("test", listFilterEntity.getName() + " " + listFilterEntity.getNo());
                if (filterResultClick != null) {
                    filterResultClick.queryChild(pNo, listFilterEntity.getChildren().get(0).getNo());
                }*/
            }
        });
        /*powerTypeAdapter = new PowerTypeAdapter(mContext, null);
        powerTypeAdapter.setItemSelect(true);
        powerTypeAdapter.setDefItem(-1);
        childView.setLayoutManager(new GridLayoutManager(mContext, 3));
        childView.setAdapter(powerTypeAdapter);
        powerTypeAdapter.setOnItemClick(new AdapterOnItemClick<AreaEntity>() {
            @Override
            public void onItemClick(AreaEntity areaEntity, int position) {
                cNo = filterEntity.getNo();
                if (filterResultClick != null) {
                    filterResultClick.queryChild(pNo, cNo);
                }
            }
        });*/
    }

    public void setData(List<AreaEntity> areaEntityList, List<FilterEntity> datas) {
        this.areaEntityList = areaEntityList;
        this.filterEntityList = datas;
        powerAreaAdapter.setDatas(areaEntityList);
    }

    private void addTitleView(String title) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.CENTER_VERTICAL;
        TextView textView = new TextView(mContext);
        textView.setText(title);
        textView.setTextSize(16);
        textView.setTextColor(mContext.getResources().getColor(R.color.color_B6B6B6));
        rootLayout.addView(textView, layoutParams);
    }

    public void setChildView(List<FilterEntity> datas, boolean isChange) {
        if (isChange) {
            powerTypeAdapterList = new ArrayList<>();
            rootLayout.removeAllViews();
            FilterEntity entity;
            for (int i = 0; i < datas.size(); i++) {
                entity = datas.get(i);
                addTitleView(entity.getName());
                MLog.d("test", "标题 " + entity.getName() + "长度" + datas.size());
                addRecyclerView(datas, i, entity.getType());
            }
        }
    }

    private void addRecyclerView(List<FilterEntity> children, final int i, final int type) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        powerTypeAdapter = new PowerTypeAdapter(mContext, null);
        powerTypeAdapterList.add(powerTypeAdapter);
        powerTypeAdapterList.get(i).setDatas(children.get(i).getAttrValList());
        powerTypeAdapterList.get(i).setItemSelect(true);
        powerTypeAdapterList.get(i).setDefItem(-1);
        childView = new MaxRecyclerView(mContext);
        childView.setNestedScrollingEnabled(false);
        childView.setLayoutManager(new GridLayoutManager(mContext, 3));
        childView.setAdapter(powerTypeAdapterList.get(i));
        powerTypeAdapterList.get(i).setOnItemClick(new AdapterOnItemClick<AreaEntity>() {
            @Override
            public void onItemClick(AreaEntity areaEntity, int position) {
//                MLog.d("test", "child " + filterEntity.getName());
                if (i == 0) {
                    dNo = areaEntity.getNo();
                } else if (i == 1) {
                    eNo = areaEntity.getNo();
                }
            }
        });
        rootLayout.addView(childView, layoutParams);
    }
}
